package org.mdrtwell.common;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

public class ImageTools {
	public static final String	PNG		= "png";
	public static final String	JPEG	= "jpeg";
	public static final String	JPG		= "jpg";
	public static final String	GIF		= "gif";

	private ImageTools() {
		super();
	}

	public static BufferedImage thumbFilter(BufferedImage source, int width, int height) {
		int iconHeight = height;
		int iconWidth = width;
		if (source.getWidth() / (double) width > source.getHeight() / (double) height) {
			iconHeight = (int) ((double) width * source.getHeight() / source.getWidth());
		} else {
			iconWidth = (int) ((double) height * source.getWidth() / source.getHeight());
		}
		// g.drawRect(0, 0, thumb.getWidth(), thumb.getHeight());
		Image buf = source.getScaledInstance(iconWidth, iconHeight, Image.SCALE_SMOOTH);
		BufferedImage thumb = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
		Graphics2D g = thumb.createGraphics();
		g.setColor(new Color(255, 255, 255, 255));
		g.drawImage(buf,
				(thumb.getWidth() - iconWidth) / 2,
				(thumb.getHeight() - iconHeight) / 2,
				null);
		// g.setColor(new Color(255, 255, 255, 200));
		// ����ƽ�������
		// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		// RenderingHints.VALUE_ANTIALIAS_ON);
		// g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
		// RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		// g.drawString("By Robin", 0, 30);
		return thumb;
	}

	/**
	 * ��ȡͼƬ
	 * 
	 * @param in
	 * @param type
	 *            png gif jpg
	 * @return
	 * @throws IOException
	 */
	public static BufferedImage readImage(InputStream in, String type) throws IOException {
		Iterator readers = ImageIO.getImageReadersByFormatName(type);
		ImageReader reader = (ImageReader) readers.next();
		// Object source; // File or InputStream
		ImageInputStream iis = ImageIO.createImageInputStream(in);
		reader.setInput(iis, true);
		BufferedImage img = reader.read(0);
		iis.close();
		return img;
	}

	public static BufferedImage readImage(String fileName) throws IOException {
		String type = fileName.substring(fileName.lastIndexOf(".") + 1);
		return readImage(new FileInputStream(fileName), type);
	}

	public static BufferedImage readImage(URL url) throws IOException {
		String type = url.toString().substring(url.toString().lastIndexOf(".") + 1);
		return readImage(url.openStream(), type);
	}

	public static BufferedImage readImage(File file) throws IOException {
		String type = file.getName().substring(file.getName().lastIndexOf(".") + 1);
		return readImage(new FileInputStream(file), type);
	}

	public static void writeImage(BufferedImage image, String filename) throws IOException {
		String type = filename.substring(filename.lastIndexOf(".") + 1);
		writeImage(image, type, filename);
	}

	public static void
			writeImage(BufferedImage image, String type, String filename) throws IOException {
		File f = new File(filename);
		writeImage(image, type, f);
	}

	public static void writeImage(BufferedImage image, String type, File file) throws IOException {
		Iterator writers = ImageIO.getImageWritersByFormatName(type);
		ImageWriter writer = (ImageWriter) writers.next();
		// Once an ImageWriter has been obtained, its destination must be set to
		// an ImageOutputStream:
		// File f = new File(filename);
		ImageOutputStream ios = ImageIO.createImageOutputStream(file);
		writer.setOutput(ios);
		// Finally, the image may be written to the output stream:
		writer.write(image);
		ios.close();
	}

	public static void
			writeImage(BufferedImage image, String type, OutputStream out) throws IOException {
		Iterator writers = ImageIO.getImageWritersByFormatName(type);
		ImageWriter writer = (ImageWriter) writers.next();
		// Once an ImageWriter has been obtained, its destination must be set to
		// an
		// ImageOutputStream:
		// File f = new File(filename);
		ImageOutputStream ios = ImageIO.createImageOutputStream(out);
		writer.setOutput(ios);
		// Finally, the image may be written to the output stream:
		writer.write(image);
		ios.close();
	}
}
